import { mapSources } from '#/assets/style';

export type BaseMapType = 'blank' | 'image' | 'terrain' | 'vector';

export const baseMapLayers = {
  vector: [
    {
      id: 'vec_mapLayer',
      name: '矢量地图',
      type: 'raster',
      source: 'vec_mapSource_map',
      layout: {
        visibility: 'visible',
      },
      organizationType: 'singleLayer',
    },
    {
      id: 'vec_annotate',
      name: '矢量注记',
      type: 'raster',
      source: 'vec_mapSource_annotate',
      layout: {
        visibility: 'visible',
      },
      organizationType: 'singleLayer',
    },
  ],
  image: [
    {
      id: 'img_mapLayer',
      name: '影像地图',
      type: 'raster',
      source: 'img_mapSource_map',
      layout: {
        visibility: 'visible',
      },
      organizationType: 'singleLayer',
    },
    // {
    //   id: 'img_annotate',
    //   name: '影像注记',
    //   type: 'raster',
    //   source: 'img_mapSource_annotate',
    //   layout: {
    //     visibility: 'visible',
    //   },
    //   organizationType: 'singleLayer',
    // },
  ],
  terrain: [
    {
      id: 'ter_mapLayer',
      name: '地形地图',
      type: 'raster',
      source: 'ter_mapSource_map',
      layout: {
        visibility: 'visible',
      },
      organizationType: 'singleLayer',
    },
    {
      id: 'ter_annotate',
      name: '地形注记',
      type: 'raster',
      source: 'ter_mapSource_annotate',
      layout: {
        visibility: 'visible',
      },
      organizationType: 'singleLayer',
    },
  ],
};
const removeLayerWithCheck = (map: any, layerId: string) => {
  if (map.getLayer(layerId)) {
    map.removeLayer(layerId);
  }
};

const removeSourceWithCheck = (map: any, sourceId: string) => {
  if (map.getSource(sourceId)) {
    map.removeSource(sourceId);
  }
};
const removeBaseMap = (map: any, type: BaseMapType) => {
  if (type === 'blank') {
    Object.values(baseMapLayers).forEach((baseMap) => {
      baseMap.forEach((layer: any) => {
        removeLayerWithCheck(map, layer.id);
        removeSourceWithCheck(map, layer.source);
      });
    });
    return;
  }
  const { [type]: _, ...otherBaseMaps } = baseMapLayers;
  Object.values(otherBaseMaps).forEach((baseMap: any) => {
    baseMap.forEach((layer: any) => {
      removeLayerWithCheck(map, layer.id);
      removeSourceWithCheck(map, layer.source);
    });
  });
};
export const switchBaseMap = (
  map: any,
  type: BaseMapType,
  lastLayerId: string,
) => {
  switch (type) {
    case 'image': {
      // 如果有注记层
      if (mapSources.imageMap[1]) {
        map.addSource(baseMapLayers.image[1]?.source, mapSources.imageMap[1]);
        map.addLayer(baseMapLayers.image[1], lastLayerId);

        map.addSource(baseMapLayers.image[0]?.source, mapSources.imageMap[0]);
        map.addLayer(baseMapLayers.image[0], baseMapLayers.image[1]?.id);
      } else {
        map.addSource(baseMapLayers.image[0]?.source, mapSources.imageMap[0]);
        map.addLayer(baseMapLayers.image[0], lastLayerId);
      }

      break;
    }
    case 'terrain': {
      map.addSource(baseMapLayers.terrain[1]?.source, mapSources.terrainMap[1]);
      map.addLayer(baseMapLayers.terrain[1], lastLayerId);

      map.addSource(baseMapLayers.terrain[0]?.source, mapSources.terrainMap[0]);

      map.addLayer(baseMapLayers.terrain[0], baseMapLayers.terrain[1]?.id);

      break;
    }
    case 'vector': {
      map.addSource(baseMapLayers.vector[1]?.source, mapSources.vectorMap[1]);
      map.addLayer(baseMapLayers.vector[1], lastLayerId);

      map.addSource(baseMapLayers.vector[0]?.source, mapSources.vectorMap[0]);
      map.addLayer(baseMapLayers.vector[0], baseMapLayers.vector[1]?.id);

      break;
    }
    // No default
  }

  // 移除除当前底图以外的其他底图（如果有）
  removeBaseMap(map, type);
};
